//
//  DoctorInfoVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/13.
//  Copyright © 2016年 阳光. All rights reserved.
//  个人信息界面

#import "DoctorInfoVC.h"
#import "ModifyProfileVC.h"
#import "ConfirmVC.h"

@interface DoctorInfoVC ()<UIScrollViewDelegate>
{
    BOOL isCN;
}

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIView *mainView;

@property(nonatomic,strong)NSDictionary *doctorInfoDic;

@end



@implementation DoctorInfoVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
      [self setMJRefreshTableView];
}

-(void)setMJRefreshTableView
{
    _scrollView.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    [_scrollView.mj_header beginRefreshing];
}


-(void)loadNewData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
        
        [self getUserInfoData];
        dispatch_async(dispatch_get_main_queue(), ^{
            //update UI
        });
    });
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor=MAIN_BACK_COLOR;
    
    self.title = NSLocalizedString(@"My Profile", nil);//@"个人信息";
    
    isCN = [[LanguageConvert alloc] hasShownIsCnOrNot];//.
    
//  [self getUserInfoData];
    
    [self initScrollView];
    
    [self setMJRefreshTableView];
    
}

-(void)getUserInfoData
{
    NSDictionary *dic=@{
                        @"doctor_id":[Util getUserDefaultObjectForKey:@"id"]
                        };
    
    [QBRequest UserInfoWithDic:dic Success:^(id res)
    {
 
        _doctorInfoDic=res[@"info"];
        
        //NSLog(@"_doctorInfoDic000 :%@", _doctorInfoDic);
        
         [self initScrollView];
        
        [_scrollView.mj_header endRefreshing];
        
    } failure:^(NSString *error)
     {
        
    }];
}

-(void)initScrollView
{
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH)];
    _scrollView.backgroundColor=MAIN_BACK_COLOR;
    _scrollView.delegate=self;
    _scrollView.scrollEnabled=YES;
    _scrollView.userInteractionEnabled=YES;
    _scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_scrollView];
    
    _mainView=[self setMainView];
    setViewCoord_Y(_mainView,0);
    [_scrollView addSubview:_mainView];
    _scrollView.contentSize=CGSizeMake(APPW,CGRectGetMaxY(self.mainView.frame));
    
}

#define cellH   kmargin*5
-(UIView *)setMainView
{
    UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(0,0,APPW,0)];
    mainView.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,kmargin);
    
    /***** 头像 *****/
    // 头像
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,kmargin*8)];
    headView.backgroundColor=WhiteColor;
    [mainView addSubview:headView];
    
    UILabel *headLabel=[mainView getLabelWithText:NSLocalizedString(@"Profile Photo", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//@"头像"
    [headView addSubview:headLabel];
    [headLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.centerY.mas_equalTo(headView.mas_centerY);
    }];
    
    // 头像
    UIImageView *headUrl=[UIImageView new];
    [headView addSubview:headUrl];
    headUrl.backgroundColor=Main_Color;
    ViewBorderRadius(headUrl,128/4,1,ClearColor);
    [headUrl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.size.mas_equalTo(CGSizeMake(128/2,128/2));
        make.centerY.mas_equalTo(headLabel.mas_centerY);
    }];
    
    [headUrl sd_setImageWithURL:[NSURL URLWithString:_doctorInfoDic[@"img"]] placeholderImage:[UIImage imageNamed:@"head"]];
    
    UIView *headLine=[UIView new];
    [headView addSubview:headLine];
    [headLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    headLine.backgroundColor=MAIN_BACK_COLOR;
    setViewHeight_Add(mainView,kmargin*8);
    
    /***** 姓名 *****/
    // 姓名
    UIView *nameView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH)];
    nameView.backgroundColor=WhiteColor;
    [mainView addSubview:nameView];
    
    UILabel *nameLeftLabel=[mainView getLabelWithText:NSLocalizedString(@"Name", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//@"姓名"
    [nameView addSubview:nameLeftLabel];
    [nameLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.centerY.mas_equalTo(nameView.mas_centerY);
    }];
    
    NSString *str;
    if ([_doctorInfoDic[@"name"] isEqualToString:@""])
    {
        str = NSLocalizedString(@"stillNo", nil);//@"暂无";
    }
    else
    {
        str=_doctorInfoDic[@"name"];
    }
    
    UILabel *nameRightLabel=[mainView getLabelWithText:str font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [nameView addSubview:nameRightLabel];
    [nameRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.centerY.mas_equalTo(nameView.mas_centerY);
    }];
    
    UIView *nameLine=[UIView new];
    [nameView addSubview:nameLine];
    [nameLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    nameLine.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,cellH);
    
    /***** 从业年份 *****/
    // 从业年份
    UIView *timeView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH)];
    timeView.backgroundColor=WhiteColor;
    [mainView addSubview:timeView];
    
    UILabel *timeLeftLabel=[mainView getLabelWithText:NSLocalizedString(@"Working Years", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [timeView addSubview:timeLeftLabel];//@"从业年份"
    [timeLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.centerY.mas_equalTo(timeView.mas_centerY);
    }];
    
    
    NSString *years;
    if (isCN == YES) {//.
        years = [NSString stringWithFormat:@"%@年",_doctorInfoDic[@"congye_times"]];
    } else{
        years = [NSString stringWithFormat:@"%@ year",_doctorInfoDic[@"congye_times"]];
    }
    UILabel *timeRightLabel=[mainView getLabelWithText:years font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [timeView addSubview:timeRightLabel];
    [timeRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.centerY.mas_equalTo(timeView.mas_centerY);
    }];
    
    UIView *timeLine=[UIView new];
    [timeView addSubview:timeLine];
    [timeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    timeLine.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,cellH);
    
    /*****  就职医院 *****/
    // 就职医院
    UIView *jobHospitalView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH)];
    jobHospitalView.backgroundColor=WhiteColor;
    [mainView addSubview:jobHospitalView];
    
    UILabel *jobHospitalLeftLabel=[mainView getLabelWithText:NSLocalizedString(@"Clinic" , nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//@"就职医院"
    [jobHospitalView addSubview:jobHospitalLeftLabel];
    [jobHospitalLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.centerY.mas_equalTo(jobHospitalView.mas_centerY);
    }];
    
    
    NSString *clinic;//.
    if (isCN == YES) {
        clinic = _doctorInfoDic[@"shop_cn_name"];
    } else{
        clinic = _doctorInfoDic[@"shop_en_name"];
    }
    UILabel *jobHospitalRightLabel=[mainView getLabelWithText:clinic font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [jobHospitalView addSubview:jobHospitalRightLabel];
    [jobHospitalRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.centerY.mas_equalTo(jobHospitalView.mas_centerY);
    }];
    
    UIView *jobHospitalLine=[UIView new];
    [jobHospitalView addSubview:jobHospitalLine];
    [jobHospitalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    jobHospitalLine.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,cellH);
    
    /*****  职称 *****/
    // 职称
    UIView *jobView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH*3/2)];
    jobView.backgroundColor=WhiteColor;
    [mainView addSubview:jobView];
    
    UILabel *jobLabel=[mainView getLabelWithText:NSLocalizedString(@"Title", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//@"职称"
    [jobView addSubview:jobLabel];
    [jobLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.top.mas_equalTo(kmargin);
    }];
    
    
    NSString *jobStr;
    if ([_doctorInfoDic[@"title_cn"] isEqualToString:@""])
    {
        jobStr = NSLocalizedString(@"stillNo", nil);//@"暂无";
    }
    else
    {
        if (isCN ==YES) {//.
             jobStr=_doctorInfoDic[@"title_cn"];
        } else{
             jobStr=_doctorInfoDic[@"title_en"];
        }
    }
    UILabel *jobTitleLabel=[mainView getLabelWithText:jobStr font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
    [jobView addSubview:jobTitleLabel];
    [jobTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.left.mas_equalTo(jobLabel.mas_left);
        make.top.mas_equalTo(jobLabel.mas_bottom).offset(kmargin);
    }];
    
    UIView *jobLine=[UIView new];
    [jobView addSubview:jobLine];
    [jobLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    jobLine.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,cellH*3/2);
    
    /*****  专业擅长 *****/
    // 专业擅长
    UIView *skillView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH*3/2)];
    skillView.backgroundColor=WhiteColor;
    [mainView addSubview:skillView];
    
    UILabel *skillLabel=[mainView getLabelWithText:NSLocalizedString(@"Speciality", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//@"专业擅长"
    [skillView addSubview:skillLabel];
    [skillLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.top.mas_equalTo(kmargin);
    }];
    
    
    NSString *skillStr;
    if ([_doctorInfoDic[@"speciality_cn"] isEqualToString:@""])
    {
        skillStr = NSLocalizedString(@"stillNo", nil);//@"暂无";
    }
    else
    {
        if (isCN == YES) {//.
            skillStr=_doctorInfoDic[@"speciality_cn"];
        } else{
            skillStr=_doctorInfoDic[@"speciality_en"];
        }
        
    }
    UILabel *skillTitleLabel=[mainView getLabelWithText:skillStr font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
    [skillView addSubview:skillTitleLabel];
    [skillTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.left.mas_equalTo(skillLabel.mas_left);
        make.top.mas_equalTo(skillLabel.mas_bottom).offset(kmargin);
    }];
    
    UIView *skillLine=[UIView new];
    [skillView addSubview:skillLine];
    [skillLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    skillLine.backgroundColor=MAIN_BACK_COLOR;
    
    setViewHeight_Add(mainView,cellH*3/2);
    
    /*****  简介 *****/
    // 简介
    UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH*3/2)];
    profileView.backgroundColor=WhiteColor;
    [mainView addSubview:profileView];
    
    UILabel *profileLabel=[mainView getLabelWithText:NSLocalizedString(@"Introduction", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//@"简介"
    [profileView addSubview:profileLabel];
    [profileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.top.mas_equalTo(kmargin);
    }];
    
    
    UIImageView *modifyProfileBtn=[UIImageView new];
    [profileView addSubview:modifyProfileBtn];
    modifyProfileBtn.image=[UIImage imageNamed:@"cell_arrow"];
    [modifyProfileBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.centerY.mas_equalTo(profileLabel.mas_centerY);
    }];
    
    
    NSString *profileStr;
    if ([_doctorInfoDic[@"cn_descript"] isEqualToString:@""])
    {
        profileStr = NSLocalizedString(@"stillNo", nil);//@"暂无";
    }
    else
    {
        if (isCN == YES) {//.
             profileStr=_doctorInfoDic[@"cn_descript"];
        } else{
             profileStr=_doctorInfoDic[@"en_descript"];
        }
    }
    UILabel *profileTitleLabel=[mainView getLabelWithText:profileStr font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
    [profileView addSubview:profileTitleLabel];
    [profileTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.left.mas_equalTo(profileLabel.mas_left);
        make.top.mas_equalTo(profileLabel.mas_bottom).offset(kmargin);
    }];
    
    UIView *profileLine=[UIView new];
    [profileView addSubview:profileLine];
    [profileLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    
    profileLine.backgroundColor=MAIN_BACK_COLOR;
    
    // 添加手势进行修改
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(modifyProfileBtnEvent)];
    [profileView addGestureRecognizer:tap];
    
    setViewHeight_Add(mainView,cellH*3/2+kmargin*3/2);
    
    /***** 执照号 *****/
    // 执照号
    UIView *codeView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH)];
    codeView.backgroundColor=WhiteColor;
    [mainView addSubview:codeView];
    
    UILabel *codeLeftLabel=[mainView getLabelWithText:NSLocalizedString(@"Medical License NO.", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//@"执照号"
    [codeView addSubview:codeLeftLabel];
    [codeLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.centerY.mas_equalTo(codeView.mas_centerY);
    }];
    
    NSString *documentStr;
    if ([_doctorInfoDic[@"document_number"] isEqualToString:@""])
    {
        documentStr = NSLocalizedString(@"stillNo", nil);//@"暂无";
    }
    else
    {
        documentStr=_doctorInfoDic[@"document_number"];
    }
    
    UILabel *codeRightLabel=[mainView getLabelWithText:documentStr font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    [codeView addSubview:codeRightLabel];
    [codeRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.centerY.mas_equalTo(codeView.mas_centerY);
    }];
    
    UIView *codeLine=[UIView new];
    [codeView addSubview:codeLine];
    [codeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW,1));
        make.bottom.mas_equalTo(0);
    }];
    codeLine.backgroundColor=MAIN_BACK_COLOR;
    setViewHeight_Add(mainView,cellH);
    
    /*****  我的医生执照 *****/
    // 我的医生执照
    UIView *photoView=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,cellH*3)];
    photoView.backgroundColor=WhiteColor;
    [mainView addSubview:photoView];
    
    UILabel *photoLabel=[mainView getLabelWithText:NSLocalizedString(@"Medical License", nil) font:Font_Number(16) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//@"我的医生执照"
    [photoView addSubview:photoLabel];
    [photoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.top.mas_equalTo(kmargin);
    }];
    
   
    UIImageView *photoURL=[[UIImageView alloc]initWithFrame:CGRectMake(kmargin,40+kmargin,160/2,160/2)];
    [photoURL sd_setImageWithURL:[NSURL URLWithString:_doctorInfoDic[@"img"]] placeholderImage:[UIImage imageNamed:@"photo"]];
    [photoView addSubview:photoURL];
  
    setViewHeight_Add(mainView,cellH*3+kmargin);

    setViewHeight_Add(mainView,kmargin*10);
    
    return mainView;
    
}


-(void)modifyProfileBtnEvent
{
    // 跳转修改简介界面
    ModifyProfileVC *modifyProfileVC=[ModifyProfileVC new];
    
    if (isCN == YES) {//...
       modifyProfileVC.profileStr=_doctorInfoDic[@"cn_descript"];
    }else {
       modifyProfileVC.profileStr=_doctorInfoDic[@"en_descript"];
    }
//    modifyProfileVC.profileStr=_doctorInfoDic[@"cn_descript"];
    
    [self.navigationController pushViewController:modifyProfileVC animated:NO];
}

@end




